<?php
/**
 * Created by PhpStorm.
 * Name：微信消息
 * User: 董坤鸿
 * Date: 2016/11/14
 * Time: 10:04
 */
namespace App\Http\Controllers;

use App\Models\Customer;
use App\Models\WxClient;
use App\Services\WxClientService;
use Illuminate\Http\Request;
use Helper;
use App\Http\Requests;

class WxclientContactMsgController extends Controller
{
    /**
     * 初始化
     * MessagesController constructor.
     */
    public function __construct()
    {
        $this->_repoService = new WxClientService();
    }

    /**
     * 聊天历史内容
     * @param WxClient $wxClient
     * @param Customer $customer
     * @return array
     */
    public function index(WxClient $wxClient, Customer $customer)
    {
        assert($wxClient, '微信客户端未找到');
        assert($customer, '微信客户端未找到');
        $contacts = array_map(function ($v) {
            return $v['customer_id'];
        }, $wxClient->contacts);
        assert(in_array($customer->_id, $contacts), '指定客户不是该微信的好友');
        $wxIds = $this->_repoService->getWxId($wxClient->id);
        if($wxIds){
            return $this->_error('您没有权限查看此微信消息');
        }
        $filter = Helper::get_filter();
        $page = request()->input('page');
        $per_page = 20;
        $pagnation = [
            'limit' => $per_page,
            'skip' => ($page - 1) * $per_page,
        ];
        $data = $this->_repoService->getMessageList($wxClient->uin, $customer, $filter, $pagnation);
        $res_data = [
            'total' => $data['total'],
            'per_page' => $per_page,
            'current_page' => intval($page),
            'data' => $data['list'],
        ];
        return $res_data;
    }

    /**
     * 获取当前登录用户可控微信号的好友列表
     * @return array|null
     */
    public function wxclientContact()
    {
        return $this->_repoService->getContacts();
    }

    /**
     * 触发处理消息的事件
     */
    public function handleMsg()
    {

    }

    /**
     * 触发处理好友的事件
     */
    public function handleContact()
    {

    }

}
